package com.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.base.BaseAction;
import com.entity.VoteUser;
import com.opensymphony.xwork2.ModelDriven;
import com.service.UserService;
import com.util.MD5;
import com.util.Page;

public class UserAction extends BaseAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String username;
	String password;
	String remenber;
	@Autowired
	UserService us;
	VoteUser vu;
	List<VoteUser> list;
	Page p = new Page();

	/*
	 * public String login() { vu = us.login(username, password); if (vu !=
	 * null) { msg = "登陆成功！"; if (vu.getStatus() == 0) {
	 * getSession().setAttribute("user", vu); return "toadmin"; } else if
	 * (vu.getStatus() == 1) { getSession().setAttribute("user", vu); return
	 * "touser"; } else { msg = "账户被锁定，登陆失败！"; return ERROR; } } msg=
	 * "用户名或密码错误，登陆失败！"; return ERROR; }
	 */
	public String login() {
		Subject subject = SecurityUtils.getSubject();
		password = MD5.getMD5(password);
		UsernamePasswordToken token = new UsernamePasswordToken(username,
				password);
		if (remenber != null) {
			token.setRememberMe(true);
		}
		try {
			msg = null;
			subject.login(token);
		} catch (UnknownAccountException e) {
			msg = "用户名或密码错误！";
		} catch (IncorrectCredentialsException e) {
			msg = "用户名或密码错误！";
		} catch (AuthenticationException e) {
			msg = "内部错误！" + e.getMessage();
		} catch (Exception e) {
			msg = "未知错误！" + e.getMessage();
		}
		if (msg == null) {
			msg = "登陆成功！";
			if (subject.isPermitted("admin")) {
				return "toadmin";
			} else if (subject.isPermitted("user")) {
				return "touser";
			} else {
				return ERROR;
			}
		} else {
			return ERROR;
		}
	}

	public String logout() {
		getSession().invalidate();
		return "logout";
	}

	public String locking() {
		vu = us.get(vu.getVuid());
		if (vu != null) {
			vu.setStatus(2);
			try {
				us.update(vu);
				msg = "锁定成功！";
				return SUCCESS;
			} catch (Exception e) {
				e.printStackTrace();
				msg = "服务器内部出错，锁定失败！";
			}
		}
		return "fail";
	}

	public String unlock() {
		vu = us.get(vu.getVuid());
		if (vu != null) {
			vu.setStatus(1);
			try {
				us.update(vu);
				msg = "解锁成功！";
				return SUCCESS;
			} catch (Exception e) {
				e.printStackTrace();
				msg = "服务器内部出错，解锁失败！";
			}
		}
		return "fail";
	}

	public String register() {
		try {
			vu.setUserpwd(MD5.getMD5(vu.getUserpwd()));
			us.save(vu);
			msg = "注册成功!";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "服务内部出错，注册失败！";
		}
		return "regjson";
	}

	public String getAll() {
		p = us.getPage(new VoteUser(), p, " and status != 0");
		return "listuser";
	}
	 private Map<String,Object> dataMap = new HashMap<String, Object>();
	public String existsuser(){
		VoteUser vu = us.getUserByName(username);
		boolean valid = false;
		if(vu==null){
			valid = true;
		}
		dataMap.put("valid", valid);
		return "existsuser";
	}
	
	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRemenber() {
		return remenber;
	}

	public void setRemenber(String remenber) {
		this.remenber = remenber;
	}

	public VoteUser getVu() {
		return vu;
	}

	public void setVu(VoteUser vu) {
		this.vu = vu;
	}

	public List<VoteUser> getList() {
		return list;
	}

	public void setList(List<VoteUser> list) {
		this.list = list;
	}

	public Page getP() {
		return p;
	}

	public void setP(Page p) {
		this.p = p;
	}




}
